//
//  CreateJobReviewInstallerDetailView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/3/7.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift
class CreateJobReviewInstallerDetailView: View{
    lazy var disposeBag = DisposeBag()
    let titleView = CreateJobReviewSectionView(R.string.localizable.technicialdetailS().uppercased(),haveEditBtnFlag: true)
    let installerBackView = View()
    let installerTitle = CreateJobReviewTitleLabel(R.string.localizable.installer())
    let installerText = CreateJobReviewContentLabel()
    let designerBackView = View()
    let designerText = CreateJobReviewContentLabel()
    let electricianBackView = View()
    let electricianText = CreateJobReviewContentLabel()
    //job site instructions for the installer
    let jobSiteInstructionBackView = View()
    let jobSiteInstructionText = CreateJobReviewContentLabel()
    //pick up required
    let pickupRequiredBackView = View()
    let selfPickupMailingAddressBackView = View()
    let selfPickupMailingAddressText = CreateJobReviewContentLabel()
    let pickupReferenceText = CreateJobReviewContentLabel()
    override func setUpView() {
        //titleView
        self.addSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        //installer
        self.addSubview(installerBackView)
        installerBackView.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom).offset(16)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        installerBackView.addSubview(installerTitle)
        installerTitle.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.size.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        installerText.textAlignment = .right
        installerBackView.addSubview(installerText)
        installerText.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.size.greaterThanOrEqualTo(0)
        }
        //designer
        self.addSubview(designerBackView)
        designerBackView.snp.makeConstraints { make in
            make.top.equalTo(installerBackView.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
            make.left.right.equalTo(installerBackView)
            make.height.greaterThanOrEqualTo(0)
        }
        let designerTitle = CreateJobReviewTitleLabel(R.string.localizable.designer())
        designerBackView.addSubview(designerTitle)
        designerTitle.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.size.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        designerBackView.addSubview(designerText)
        designerText.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.size.greaterThanOrEqualTo(0)
        }
        //electrician
        self.addSubview(electricianBackView)
        electricianBackView.snp.makeConstraints { make in
            make.top.equalTo(designerBackView.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
            make.left.right.equalTo(installerBackView)
            make.height.greaterThanOrEqualTo(0)
        }
        let electricianTitle = CreateJobReviewTitleLabel(R.string.localizable.electrician())
        electricianBackView.addSubview(electricianTitle)
        electricianTitle.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.size.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        electricianBackView.addSubview(electricianText)
        electricianText.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.size.greaterThanOrEqualTo(0)
        }
        //2.jobSiteInstruction for the instaler
        self.addSubview(jobSiteInstructionBackView)
        jobSiteInstructionBackView.snp.makeConstraints { make in
            make.top.equalTo(electricianBackView.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
            make.height.greaterThanOrEqualTo(0)
            make.left.right.equalTo(installerBackView)
        }
        //
        //jobSiteInstructionLineView
        let jobSiteInstructionLineView = View(hexStr: "#EDEDED")
        jobSiteInstructionBackView.addSubview(jobSiteInstructionLineView)
        jobSiteInstructionLineView.snp.makeConstraints { make in
            make.top.right.left.equalToSuperview()
            make.size.equalTo(1)
        }
        let jobSiteInstructionTitle = CreateJobReviewTitleLabel(R.string.localizable.jobSiteInstructionsForTheInstaller())
        jobSiteInstructionBackView.addSubview(jobSiteInstructionTitle)
        jobSiteInstructionTitle.snp.makeConstraints { make in
            make.top.equalTo(jobSiteInstructionLineView.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
            make.left.equalToSuperview()
            make.size.greaterThanOrEqualTo(0)
        }
        jobSiteInstructionText.numberOfLines = 0
        jobSiteInstructionBackView.addSubview(jobSiteInstructionText)
        jobSiteInstructionText.snp.makeConstraints { make in
            make.top.equalTo(jobSiteInstructionTitle.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalTo(-CREATEJOB_REVIEW_INTERNAL)
        }
        //3.pick up required view
        self.addSubview(pickupRequiredBackView)
        pickupRequiredBackView.snp.makeConstraints { make in
            make.top.equalTo(jobSiteInstructionBackView.snp.bottom)
            make.height.greaterThanOrEqualTo(0)
            make.left.equalTo(installerTitle)
            make.right.equalTo(installerText)
            make.bottom.equalToSuperview()
        }
        //pickupRequiredLineView
        let pickupRequiredLineView = View(hexStr: "#EDEDED")
        pickupRequiredBackView.addSubview(pickupRequiredLineView)
        pickupRequiredLineView.snp.makeConstraints { make in
            make.top.right.left.equalToSuperview()
            make.height.equalTo(1)
        }
        //
        pickupRequiredBackView.addSubview(selfPickupMailingAddressBackView)
        selfPickupMailingAddressBackView.snp.makeConstraints { make in
            make.top.equalTo(CREATEJOB_REVIEW_INTERNAL)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        
        let selfPickupMailingAddressTitle = CreateJobReviewTitleLabel(R.string.localizable.selfPickupMailingAddress())
        selfPickupMailingAddressTitle.numberOfLines = 2
        selfPickupMailingAddressTitle.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        selfPickupMailingAddressTitle.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        selfPickupMailingAddressBackView.addSubview(selfPickupMailingAddressTitle)
        selfPickupMailingAddressTitle.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.size.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        selfPickupMailingAddressText.textAlignment = .right
        selfPickupMailingAddressText.numberOfLines = 0
        selfPickupMailingAddressBackView.addSubview(selfPickupMailingAddressText)
        selfPickupMailingAddressText.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.left.equalTo(selfPickupMailingAddressTitle.snp.right).offset(5)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        let pickupReferenceTitle = CreateJobReviewTitleLabel(R.string.localizable.pickUpReference())
        pickupReferenceTitle.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        pickupReferenceTitle.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        pickupRequiredBackView.addSubview(pickupReferenceTitle)
        pickupReferenceTitle.snp.makeConstraints { make in
            make.top.equalTo(selfPickupMailingAddressBackView.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
            make.left.equalTo(selfPickupMailingAddressTitle)
            make.size.greaterThanOrEqualTo(0)
        }
        pickupReferenceText.textAlignment = .right
        pickupReferenceText.numberOfLines = 0
        pickupRequiredBackView.addSubview(pickupReferenceText)
        pickupReferenceText.snp.makeConstraints { make in
            make.top.equalTo(pickupReferenceTitle)
            make.left.equalTo(pickupReferenceTitle.snp.right).offset(5)
            make.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(pickupReferenceTitle)
            make.bottom.equalTo(-CREATEJOB_REVIEW_INTERNAL)
        }
        self.initBiding()
    }
    private func initBiding(){
        //installer
        CreateJobModelHelper.shared.installerDetailObserver.map({$0.installerName}).subscribe { [weak self] name in
            guard let strongSelf = self else {return }
            strongSelf.installerText.text = name
        }.disposed(by: self.disposeBag)
        CreateJobModelHelper.shared.installerDetailObserver.map({$0.installer_valid_flag}).subscribe { [weak self] validFlag in
            guard let strongSelf = self else {return }
            strongSelf.installerBackView.backgroundColor = (validFlag ? .white : UIColor(hexStr: "#ED3E3E1A"))
        }.disposed(by: self.disposeBag)
        //designer
        CreateJobModelHelper.shared.installerDetailObserver.map({$0.designerName}).subscribe { [weak self] name in
            guard let strongSelf = self else {return }
            strongSelf.designerText.text = name
        }.disposed(by: self.disposeBag)
        CreateJobModelHelper.shared.installerDetailObserver.map({$0.designer_valid_flag}).subscribe { [weak self] validFlag in
            guard let strongSelf = self else {return }
            strongSelf.designerBackView.backgroundColor = (validFlag ? .white : UIColor(hexStr: "#ED3E3E1A"))
        }.disposed(by: self.disposeBag)
        //electrician
        CreateJobModelHelper.shared.installerDetailObserver.map({$0.electricianName}).subscribe { [weak self] name in
            guard let strongSelf = self else {return }
            strongSelf.electricianText.text = name
        }.disposed(by: self.disposeBag)
        CreateJobModelHelper.shared.installerDetailObserver.map({$0.electrician_valid_flag}).subscribe { [weak self] validFlag in
            guard let strongSelf = self else {return }
            strongSelf.electricianBackView.backgroundColor = (validFlag ? .white : UIColor(hexStr: "#ED3E3E1A"))
        }.disposed(by: self.disposeBag)
        //installer_job_site_instruction
        CreateJobModelHelper.shared.installerDetailObserver.map({$0.installer_job_site_instruction}).subscribe { [weak self] data in
            guard let strongSelf = self else {return }
            if data.count > 0{//有数据，就显示
                strongSelf.jobSiteInstructionBackView.isHidden = false
                strongSelf.jobSiteInstructionBackView.snp.remakeConstraints { make in
                    make.top.equalTo(strongSelf.electricianText.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
                    make.height.greaterThanOrEqualTo(0)
                    make.left.equalTo(strongSelf.installerTitle)
                    make.right.equalTo(strongSelf.installerText)
                }
                strongSelf.jobSiteInstructionText.text = data
            }else{//无数据隐藏
                strongSelf.jobSiteInstructionBackView.isHidden = true
                strongSelf.jobSiteInstructionBackView.snp.remakeConstraints { make in
                    make.bottom.equalTo(strongSelf.electricianText.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
                    make.height.greaterThanOrEqualTo(0)
                    make.left.right.equalToSuperview()
                }
            }
        }.disposed(by: self.disposeBag)
        //pickup requires
        CreateJobModelHelper.shared.installerDetailObserver.map({$0.pickupRequired}).subscribe { [weak self] pickupRequired in
            guard let strongSelf = self else {return }
            if pickupRequired{//开启pick up required，就显示
                strongSelf.pickupRequiredBackView.isHidden = false
                strongSelf.pickupRequiredBackView.snp.remakeConstraints { make in
                    make.top.equalTo(strongSelf.jobSiteInstructionBackView.snp.bottom)
                    make.height.greaterThanOrEqualTo(0)
                    make.left.equalTo(strongSelf.installerTitle)
                    make.right.equalTo(strongSelf.installerText)
                    make.bottom.equalToSuperview()
                }
                strongSelf.selfPickupMailingAddressText.text = CreateJobModelHelper.shared.createJobViewModel.installerDetail.delivery_address_text
                strongSelf.pickupReferenceText.text =  CreateJobModelHelper.shared.createJobViewModel.installerDetail.pickupReference
            }else{//未开启pick up required，就隐藏
                strongSelf.pickupRequiredBackView.isHidden = true
                strongSelf.pickupRequiredBackView.snp.remakeConstraints { make in
                    make.bottom.equalTo(strongSelf.jobSiteInstructionBackView.snp.bottom)
                    make.height.greaterThanOrEqualTo(0)
                    make.left.right.bottom.equalToSuperview()
                }
            }
        }.disposed(by: self.disposeBag)
        //pick up address是否有效
        CreateJobModelHelper.shared.installerDetailObserver.map({$0.delivery_address_valid_flag}).subscribe { [weak self] validFlag in
            guard let strongSelf = self else {return }
            strongSelf.selfPickupMailingAddressBackView.backgroundColor = (validFlag ? .white : UIColor(hexStr: "#ED3E3E1A"))
        }.disposed(by: self.disposeBag)
    }
}
